package com.httam.taxiSmart;

import java.util.ArrayList;

import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.ProgressBar;

public class ListTaxiActivity extends Activity implements OnItemClickListener {

	// Handle widgets UI
	private ListView lvTaxi;
	private ProgressBar mProgress;
	private EditText txtSearch;
	SQLiteDatabase database;
	ArrayList<String> arrListTaxi = null;
	MyCustomAdapter mCustomAdapter = null;

	Integer[] thumbnails = { R.drawable.taxi_saigon, R.drawable.taxi_mailinh,
			R.drawable.taxi_vinasun, R.drawable.taxi_vina,
			R.drawable.taxi_savico, R.drawable.taxi_hoanlong,
			R.drawable.taxi_saigon_petrolimet, R.drawable.taxi_airport,
			R.drawable.taxi_rangdong };

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_list_taxi);

		lvTaxi = (ListView) findViewById(R.id.listTaxi);
		lvTaxi.setOnItemClickListener(this);
		mProgress = (ProgressBar) findViewById(R.id.progressBar);

		LoadData mLoadData = new LoadData();
		mLoadData.execute();

		txtSearch = (EditText) findViewById(R.id.txtAutoSearch);
		txtSearch.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
				String strSearch = txtSearch.getText().toString();
				String sql = "select * from HangTaxi where TenHang like '%"
						+ strSearch + "%'";
				loadListTaxi(sql);
			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
			}

			@Override
			public void afterTextChanged(Editable s) {
			}
		});
	}

	private class LoadData extends AsyncTask<Void, String, ArrayList<String>> {

		@Override
		protected void onPreExecute() {

			super.onPreExecute();
			mProgress.setVisibility(View.VISIBLE);

		}

		@Override
		protected ArrayList<String> doInBackground(Void... params) {
			database = openOrCreateDatabase(MainActivity.pathSQLite,
					MODE_PRIVATE, null);
			Cursor curSor = database.query("HangTaxi", null, null, null, null,
					null, null);

			arrListTaxi = new ArrayList<String>();
			Integer[] thumnails;
			thumnails = new Integer[9];
			int index = 0;
			curSor.moveToFirst();
			while (!curSor.isAfterLast()) {
				String data = curSor.getString(1) + "\n" + curSor.getString(2);
				thumnails[index] = ChooseImage(curSor.getString(1));
				arrListTaxi.add(data);
				curSor.moveToNext();
				index++;
			}

			curSor.close();
			return arrListTaxi;
		}

		@Override
		protected void onPostExecute(ArrayList<String> result) {

			super.onPostExecute(result);
			mProgress.setVisibility(View.GONE);
			mCustomAdapter = new MyCustomAdapter(ListTaxiActivity.this,
					R.layout.activity_custom_list, result, thumbnails);
			lvTaxi.setAdapter(mCustomAdapter);
		}

	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
	}

	/**
	 * This method load list taxi by query
	 * 
	 * @param query
	 */
	private void loadListTaxi(String query) {
		database = openOrCreateDatabase(MainActivity.pathSQLite, MODE_PRIVATE,
				null);
		Cursor curSor = database.rawQuery(query, null);

		lvTaxi = (ListView) findViewById(R.id.listTaxi);
		arrListTaxi = new ArrayList<String>();
		Integer[] thumbnails;
		thumbnails = new Integer[9];
		int index = 0;

		curSor.moveToFirst();
		while (!curSor.isAfterLast()) {
			String data = curSor.getString(1) + "\n" + curSor.getString(2);
			thumbnails[index] = ChooseImage(curSor.getString(1));
			arrListTaxi.add(data);
			curSor.moveToNext();
			index++;
		}

		curSor.close();

		mCustomAdapter = new MyCustomAdapter(this,
				R.layout.activity_custom_list, arrListTaxi, thumbnails);
		lvTaxi.setAdapter(mCustomAdapter);
		lvTaxi.setOnItemClickListener(this);
	}

	/** get property image resource */
	Integer ChooseImage(String HangTaxi) {
		if (HangTaxi.equals("Sài Gòn Taxi")) {
			return R.drawable.taxi_saigon;
		}
		if (HangTaxi.equals("Mai Linh")) {
			return R.drawable.taxi_mailinh;
		}
		if (HangTaxi.equals("VinaSun")) {
			return R.drawable.taxi_vinasun;
		}
		if (HangTaxi.equals("Vina Taxi")) {
			return R.drawable.taxi_vina;
		}
		if (HangTaxi.equals("Savico Taxi")) {
			return R.drawable.taxi_savico;
		}
		if (HangTaxi.equals("Hoang Long")) {
			return R.drawable.taxi_hoanlong;
		}
		if (HangTaxi.equals("Sai Gon Petrolimex")) {
			return R.drawable.taxi_saigon_petrolimet;
		}
		if (HangTaxi.equals("Taxi Airport")) {
			return R.drawable.taxi_airport;
		}
		if (HangTaxi.equals("Taxi Rạng Đông")) {
			return R.drawable.taxi_rangdong;
		}
		return R.drawable.taxi_rangdong;
	}

}
